// pages/userinfo/userinfo.js
var app = getApp()
import WeCropper from '../../utils/weCropper/we-cropper.min.js'
const FormData = require('../../utils/fromData/formData.js')
// import FormData from '../../utils/formData/formData.js'
const device = wx.getSystemInfoSync() // 获取设备信息
const width = device.windowWidth
const height = device.windowHeight - 100;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    genderArray: ['男', '女'],
    genderIndex: 0,
    date: '',
    userinfo: {
      photo: "https://api.uuz.bid/null.jpg",
      name: 'null',
      gender: 0,
      birthday: '2001-03-27'
    },
    inputName: '',
    maxlength: 7,
    dailog: false,
    showWeCropper: false,
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 300,
        height: 300
      }
    }
  },
  shwoDailog(){
    this.setData({
      dailog: true,
      inputName: this.data.userinfo.name
    })
  },
  closeDailog(){
    this.setData({
      dailog: false
    })
  },
  saveUserName(){
    this.setData({
      userinfo: {...this.data.userinfo, name: this.data.inputName}
    })
    this.uploadInfo(()=>{
      this.closeDailog()
    })
    console.log(123);
  },
  genderChange(e) {
    console.log('性别为', e.detail.value)
    this.setData({
      userinfo: {...this.data.userinfo, gender: e.detail.value}
    })
    this.uploadInfo()
  },
  birthdayChange: function(e) {
    console.log('生日值为', e.detail.value)
    this.setData({
      userinfo: {...this.data.userinfo, birthday: e.detail.value}
    })
    this.uploadInfo()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
    this.initWeCropper()
  },
  getUserInfo(){
    console.log(app.globalData.Authorization);
    wx.request({
      url: app.globalData.baseURL + '/user/profile',
        method: 'GET',
        header: {
          Authorization: app.globalData.Authorization
        },
        success: (res) => {
          console.log(res);
          const newData = {
            photo: res.data.data.photo,
            name: res.data.data.name,
            gender: res.data.data.gender,
            birthday: res.data.data.birthday
          }
          this.setData({
            userinfo: newData,
          })
        }
    })
  },
  uploadInfo(callback){
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: app.globalData.baseURL + '/user/profile',
      method: 'PATCH',
      header: {
        Authorization: app.globalData.Authorization
      },
      data: this.data.userinfo,
      success: (res)=> {
        console.log(res);
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(()=>{
          wx.hideToast()
        },1000)
        callback && callback()
      },
      complete: ()=> {
        wx.hideLoading()
      }
    })
  },
  // 初始化weCropper插件图
  initWeCropper() {
    const {
      cropperOpt
    } = this.data
    this.cropper = new WeCropper(cropperOpt)
      .on('ready', (ctx) => {})
      .on('beforeImageLoad', (ctx) => {
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        wx.hideToast()
      })
  },

  //选择图片
  chooseimg() {
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          showWeCropper: true
        });
        this.cropper.pushOrign(res.tempFiles[0].tempFilePath);
      }
    })
  },

  touchStart(e) {
    this.cropper.touchStart(e)
  },
  touchMove(e) {
    this.cropper.touchMove(e)
  },
  touchEnd(e) {
    this.cropper.touchEnd(e)
  },

  // 获取图片链接
  getCropperImage() {
    wx.showLoading({
      title: '上传中',
    })
    this.cropper.getCropperImage((res) => {
	  let formData = new FormData();
    // formData.appendFile("photo", wx.getFileSystemManager().readFileSync(res));
    formData.appendFile("photo", res);
	  let data = formData.getData();
      wx.request({
      // wx.uploadFile({
      //   filePath: res,
      //   name: 'photo',
        url: app.globalData.baseURL + '/user/photo',
        method: 'PATCH',
        header: {
          'Authorization': app.globalData.Authorization,
          'content-type': data.contentType
        },
        data: data.buffer,
        success: (res)=> {
          this.setData({
            showWeCropper: false,
            userinfo: {...this.data.userinfo, photo: res.data.data.photo}
          })
          console.log(res)
        },
        complete: ()=>{
          wx.hideLoading()
        }
      })
    })
  },
  closeUpload(){
    this.setData({
      showWeCropper: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})